package org.edith.shiro.dao;

import org.apache.ibatis.annotations.*;
import org.edith.shiro.dataobject.SysMenuDO;

import java.util.List;

/**
 * 权限DAO
 */
public interface SysMenuDAO {

    /**
     * 根据角色查询用户权限
     *
     * @param roleId 角色ID
     *
     * @return List<SysMenuDO> 权限集合
     */
    @Results(value = {
                @Result(column = "menu_id", property = "menuId"),
                @Result(column = "parent_id", property = "parentId"),
                @Result(column = "name", property = "name"),
                @Result(column = "url", property = "url"),
                @Result(column = "perms", property = "perms"),
                @Result(column = "icon", property = "icon"),
            @Result(column = "remark", property = "remark"),
            @Result(column = "create_time", property = "createTime")
            }
    )
    @Select("select * from sys_menu where perms is not null and menu_id in (select menu_id from sys_role_menu where role_id =#{roleId})")
    List<SysMenuDO> selectSysMenuByRoleId(Long roleId);

    @Insert("        create table  if not exists sys_menu(\n" +
            "          menu_id     bigint auto_increment comment '权限ID' primary key,\n" +
            "          parent_id   bigint null comment '父级菜单ID',\n" +
            "          name  varchar(50) null comment '权限名称',\n" +
            "          perms varchar(500) null comment '权限标识',\n" +
            "          url varchar(256) null comment 'url地址',\n" +
            "          create_time datetime default CURRENT_TIMESTAMP null comment '创建时间',\n" +
            "          icon varchar(256) null comment '图标'\n" +
            "          remark varchar(256) null comment '备注'\n" +
            "        ) comment '权限表' charset = utf8;")
    void createTable();
}
